/***********************************************************************************
Copyright (c) Nordic Semiconductor ASA
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

  3. Neither the name of Nordic Semiconductor ASA nor the names of other
  contributors to this software may be used to endorse or promote products
  derived from this software without specific prior written permission.

  4. This software must only be used in a processor manufactured by Nordic
  Semiconductor ASA, or in a processor manufactured by a third party that
  is used in combination with a processor manufactured by Nordic Semiconductor.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************/


#include "nrf_adv_conn.h"


#include "boards.h"
#include "nrf_sdm.h"
#include "app_error.h"	
#include "simple_uart.h"
#include "nrf_advertiser.h"
#include "nrf_assert.h"
#include "advertise.h"
#include "nrf_gpio.h"
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

/*****************************************************************************
* Local definitions
*****************************************************************************/

/**@brief Disable logging to UART by commenting out this line. It is recomended to do this if
 * if you want to study timing in the example using a logic analyzer.
 */
#define USE_UART_LOGGING

/**@brief Macro defined to output log data on the UART or not, based on the USE_UART_LOGGING flag.
 * If logging is disabled, it will just yield a NOP instruction.
 */
#ifdef USE_UART_LOGGING
#define __LOG(F, ...) (test_logf("TIMESLOT_TEST_LOG: %s: %d: " #F "\r\n", __FILE__, __LINE__, ##__VA_ARGS__))
#else
  #define __LOG(F, ...) (void)__NOP()
#endif

#define BLE_ADV_INTERVAL_100MS 0x00A0
#define BLE_ADV_INTERVAL_150MS 0x00F0


static uint32_t g_sd_assert_pc;



/**@brief Global variables used for storing assert information from the timeslot event handler.
 */
static uint32_t g_evt;


/*****************************************************************************
* Static Functions
*****************************************************************************/
/*statr clock*/
static void clock_init(void);

/**@brief Callback handlers
 */
/**
* Assert callback for Softdevice. Sends message over USB UART (at baud38400, 8n1, RTS/CTS)
* indicating file and line number. Is blocking, as the timeslot API probably would not be
* able to recover from this. Note that an assert in Include/softdevice_timeslot.c, line 192
* is most probably related to a prematurely aborted timeslot 
*/


/**@brief Local function prototypes.
 */
static void test_logf(const char *fmt, ...);



static void clock_init(void)
{
	 // Start 16 MHz crystal oscillator.
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART    = 1;

    // Wait for the external oscillator to start up.
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
    {
        // Do nothing.
    }

}

int main(void)
            {
  /* Silence the compiler */
 // (void) g_sd_assert_pc;
 // (void) g_evt;
  
	clock_init();
	
	simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, false);
	
	//char start_msg[128];
	//sprintf(&start_msg[0], "\n| %s |---------------------------------------------------\n\n", __TIME__);
	//simple_uart_putstring((uint8_t*) &start_msg[0]);
	nrf_gpio_range_cfg_output(0, 20);
	adv_start();
	 
  while(true){
	
	}
	
	
}



/**@brief Logging function, used for formated output on the UART.
 */
void test_logf(const char *fmt, ...)
{
  int16_t res = 0;
  static uint8_t buf[150];
  
  va_list args;
  va_start(args, fmt);
  
 // res = vsnprintf((char*) buf, sizeof(buf), fmt,  args);
 // ASSERT(res >= 0 && res <= (sizeof buf) - 1);

  //simple_uart_putstring(buf);///test
  
  va_end(args);
}
